package com.jjb.www.ui.activity;

import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.bumptech.glide.load.MultiTransformation;
import com.bumptech.glide.load.resource.bitmap.CenterCrop;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.hjq.base.BaseDialog;
import com.hjq.http.EasyHttp;
import com.hjq.http.listener.HttpCallback;
import com.hjq.shape.layout.ShapeLinearLayout;
import com.hjq.shape.view.ShapeImageView;
import com.hjq.shape.view.ShapeTextView;
import com.hjq.widget.layout.WrapRecyclerView;
import com.jjb.www.R;
import com.jjb.www.app.AppActivity;
import com.jjb.www.app.AppAdapter;
import com.jjb.www.http.api.CurriculumSortApi;
import com.jjb.www.http.api.FeedbackDetailsApi;
import com.jjb.www.http.api.ProblemScoreApi;
import com.jjb.www.http.glide.GlideApp;
import com.jjb.www.http.model.HttpData;
import com.jjb.www.http.model.HttpList;
import com.jjb.www.http.model.HttpListData;
import com.jjb.www.ui.adapter.BackDetailsAdapter;
import com.jjb.www.ui.adapter.ImgListAdapter;
import com.jjb.www.ui.dialog.PerfectInfoDialog;
import com.jjb.www.ui.fragment.CourseListFragment;
import com.jjb.www.widget.CommonListRefreshView;
import com.jjb.www.widget.CommonRefreshView;

import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;

/**
 *    author : Android CRG
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2019/03/01
 *    desc   : 反馈详情
 */
public class FeedbackDetailsActivity extends AppActivity {
    private BackDetailsAdapter adapter;
    private ImgListAdapter imgListAdapter;
    private List<String> list = new ArrayList<>();
    private ShapeTextView mTvReply;
    private ShapeTextView mTvEvaluate;
    private ShapeLinearLayout mLlBtn;
    private String id = "";
    private View mHeadView;
    private WrapRecyclerView recyclerView;

    private ShapeImageView mImgHead;
    private ShapeTextView mTvName;
    private WrapRecyclerView mRvImg;
    private ShapeTextView mTvTime;



    @Override
    protected int getLayoutId() {
        return R.layout.activity_feedback_details;
    }

    @Override
    protected void initView() {
        mTvReply = findViewById(R.id.tv_reply);
        mLlBtn = findViewById(R.id.ll_btn);
        mTvEvaluate = findViewById(R.id.tv_evaluate);
        recyclerView = findViewById(R.id.refreshView);
        adapter = new BackDetailsAdapter(getContext());
        recyclerView.setAdapter(adapter);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mHeadView = LayoutInflater.from(getContext()).inflate(R.layout.item_feedback_details,
                recyclerView, false);
        recyclerView.addHeaderView(mHeadView);

        mImgHead = mHeadView.findViewById(R.id.img_head);
        mTvName = mHeadView.findViewById(R.id.tv_name);
        mRvImg = mHeadView.findViewById(R.id.rv_img);
        mTvTime = mHeadView.findViewById(R.id.tv_time);
        imgListAdapter = new ImgListAdapter(getContext());

        mRvImg.setLayoutManager(new GridLayoutManager(getContext(),3));
        mRvImg.setAdapter(imgListAdapter);
        for (int i = 1; i < 11; i++) {
            list.add(i+"分");
        }
        setOnClickListener(mTvReply,mTvEvaluate);
    }

    @Override
    protected void onResume() {
        super.onResume();
        id = getString("id");
        getList();
    }

    @Override
    protected void initData() {

    }

    private void getList() {
        EasyHttp.get(this)
                .api(new FeedbackDetailsApi()
                .setId(id))
                .request(new HttpCallback<HttpData<FeedbackDetailsApi.Bean>>(this) {

                    @Override
                    public void onSucceed(HttpData<FeedbackDetailsApi.Bean> data) {
                        GlideApp.with(getActivity())
                                .load(data.getData().getInfo().getAvatar_id())
                                .transform(new MultiTransformation<>(new CenterCrop(), new CircleCrop()))
                                .into(mImgHead);
                        mTvName.setText(data.getData().getInfo().getContent());
                        mTvTime.setText(data.getData().getInfo().getCreate_time());
                        imgListAdapter.setData(data.getData().getInfo().getImg());
                        adapter.setData(data.getData().getList());
                        ////0 代回复 1 已回复 2 已完成
                        if (data.getData().getInfo().getStatus()==2){
                            mLlBtn.setVisibility(View.GONE);
                        }
                    }

                });
    }

    @Override
    public void onClick(View view) {
        if (view==mTvEvaluate){
            new PerfectInfoDialog.Builder(this)
                    .setList(list)
                    .setListener(new PerfectInfoDialog.OnListener<String>() {

                        @Override
                        public void onCompleted(BaseDialog dialog, String label, int currentPosition) {
                            scoring(currentPosition);
                        }

                        @Override
                        public void onCancel(BaseDialog dialog) {

                        }
                    }).show();
        }else if (view==mTvReply){
            Intent intent = new Intent();
            intent.putExtra("id",id);
            intent.putExtra("fromType","details");
            intent.setClass(getContext(),ReplyActivity.class);
            startActivity(intent);
        }
    }

    private void scoring(int currentPosition) {
        EasyHttp.post(this)
                .api(new ProblemScoreApi()
                .setId(id)
                .setScore((currentPosition+1)+""))
                .request(new HttpCallback<HttpData<Void>>(this) {

                    @Override
                    public void onSucceed(HttpData<Void> data) {
                        toast("评价成功");
                        finish();
                    }

                });
    }
}